3 * Block restriction interface.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Block
;
25 use MediaWiki\Block\Restriction\NamespaceRestriction
;
26 use MediaWiki\Block\Restriction\PageRestriction
;
27 use MediaWiki\Block\Restriction\Restriction
;
28 use Wikimedia\Rdbms\IResultWrapper
;
29 use Wikimedia\Rdbms\IDatabase
;
31 class BlockRestriction
{
34 * Map of all of the restriction types.
36 private static $types = [
37 PageRestriction
::TYPE_ID
=> PageRestriction
::class,
38 NamespaceRestriction
::TYPE_ID
=> NamespaceRestriction
::class,
42 * Retrieves the restrictions from the database by block id.
45 * @param int|array $blockId
46 * @param IDatabase|null $db
47 * @return Restriction[]
49 public static function loadByBlockId( $blockId, IDatabase
$db = null ) {
50 if ( $blockId === null ||
$blockId === [] ) {
54 $db = $db ?
: wfGetDb( DB_REPLICA
);
56 $result = $db->select(
57 [ 'ipblocks_restrictions', 'page' ],
58 [ 'ir_ipb_id', 'ir_type', 'ir_value', 'page_namespace', 'page_title' ],
59 [ 'ir_ipb_id' => $blockId ],
62 [ 'page' => [ 'LEFT JOIN', [ 'ir_type' => PageRestriction
::TYPE_ID
, 'ir_value=page_id' ] ] ]
65 return self
::resultToRestrictions( $result );
69 * Inserts the restrictions into the database.
72 * @param Restriction[] $restrictions
75 public static function insert( array $restrictions ) {
76 if ( empty( $restrictions ) ) {
81 foreach ( $restrictions as $restriction ) {
82 if ( !$restriction instanceof Restriction
) {
85 $rows[] = $restriction->toRow();
88 if ( empty( $rows ) ) {
92 $dbw = wfGetDB( DB_MASTER
);
95 'ipblocks_restrictions',
103 * Updates the list of restrictions. This method does not allow removing all
104 * of the restrictions. To do that, use ::deleteByBlockId().
107 * @param Restriction[] $restrictions
110 public static function update( array $restrictions ) {
111 $dbw = wfGetDB( DB_MASTER
);
113 $dbw->startAtomic( __METHOD__
);
115 // Organize the restrictions by blockid.
116 $restrictionList = self
::restrictionsByBlockId( $restrictions );
118 // Load the existing restrictions and organize by block id. Any block ids
119 // that were passed into this function will be used to load all of the
120 // existing restrictions. This list might be the same, or may be completely
123 $blockIds = array_keys( $restrictionList );
124 if ( !empty( $blockIds ) ) {
125 $result = $dbw->select(
126 [ 'ipblocks_restrictions' ],
127 [ 'ir_ipb_id', 'ir_type', 'ir_value' ],
128 [ 'ir_ipb_id' => $blockIds ],
133 $existingList = self
::restrictionsByBlockId(
134 self
::resultToRestrictions( $result )
139 // Perform the actions on a per block-id basis.
140 foreach ( $restrictionList as $blockId => $blockRestrictions ) {
141 // Insert all of the restrictions first, ignoring ones that already exist.
142 $success = self
::insert( $blockRestrictions );
144 // Update the result. The first false is the result, otherwise, true.
145 $result = $success && $result;
147 $restrictionsToRemove = self
::restrictionsToRemove(
148 $existingList[$blockId] ??
[],
152 if ( empty( $restrictionsToRemove ) ) {
156 $success = self
::delete( $restrictionsToRemove );
158 // Update the result. The first false is the result, otherwise, true.
159 $result = $success && $result;
162 $dbw->endAtomic( __METHOD__
);
168 * Updates the list of restrictions by parent id.
171 * @param int $parentBlockId
172 * @param Restriction[] $restrictions
175 public static function updateByParentBlockId( $parentBlockId, array $restrictions ) {
176 // If removing all of the restrictions, then just delete them all.
177 if ( empty( $restrictions ) ) {
178 return self
::deleteByParentBlockId( $parentBlockId );
181 $parentBlockId = (int)$parentBlockId;
183 $db = wfGetDb( DB_MASTER
);
185 $db->startAtomic( __METHOD__
);
187 $blockIds = $db->selectFieldValues(
190 [ 'ipb_parent_block_id' => $parentBlockId ],
196 foreach ( $blockIds as $id ) {
197 $success = self
::update( self
::setBlockId( $id, $restrictions ) );
198 // Update the result. The first false is the result, otherwise, true.
199 $result = $success && $result;
202 $db->endAtomic( __METHOD__
);
208 * Delete the restrictions.
211 * @param Restriction[]|null $restrictions
212 * @throws MWException
215 public static function delete( array $restrictions ) {
216 $dbw = wfGetDB( DB_MASTER
);
218 foreach ( $restrictions as $restriction ) {
219 if ( !$restriction instanceof Restriction
) {
223 $success = $dbw->delete(
224 'ipblocks_restrictions',
225 // The restriction row is made up of a compound primary key. Therefore,
226 // the row and the delete conditions are the same.
227 $restriction->toRow(),
230 // Update the result. The first false is the result, otherwise, true.
231 $result = $success && $result;
238 * Delete the restrictions by Block ID.
241 * @param int|array $blockId
242 * @throws MWException
245 public static function deleteByBlockId( $blockId ) {
246 $dbw = wfGetDB( DB_MASTER
);
248 'ipblocks_restrictions',
249 [ 'ir_ipb_id' => $blockId ],
255 * Delete the restrictions by Parent Block ID.
258 * @param int|array $parentBlockId
259 * @throws MWException
262 public static function deleteByParentBlockId( $parentBlockId ) {
263 $dbw = wfGetDB( DB_MASTER
);
264 return $dbw->deleteJoin(
265 'ipblocks_restrictions',
269 [ 'ipb_parent_block_id' => $parentBlockId ],
275 * Checks if two arrays of Restrictions are effectively equal. This is a loose
276 * equality check as the restrictions do not have to contain the same block
280 * @param Restriction[] $a
281 * @param Restriction[] $b
284 public static function equals( array $a, array $b ) {
285 $filter = function ( $restriction ) {
286 return $restriction instanceof Restriction
;
289 // Ensure that every item in the array is a Restriction. This prevents a
290 // fatal error from calling Restriction::getHash if something in the array
291 // is not a restriction.
292 $a = array_filter( $a, $filter );
293 $b = array_filter( $b, $filter );
295 $aCount = count( $a );
296 $bCount = count( $b );
298 // If the count is different, then they are obviously a different set.
299 if ( $aCount !== $bCount ) {
303 // If both sets contain no items, then they are the same set.
304 if ( $aCount === 0 && $bCount === 0 ) {
308 $hasher = function ( $r ) {
309 return $r->getHash();
312 $aHashes = array_map( $hasher, $a );
313 $bHashes = array_map( $hasher, $b );
318 return $aHashes === $bHashes;
322 * Set the blockId on a set of restrictions and return a new set.
325 * @param int $blockId
326 * @param Restriction[] $restrictions
327 * @return Restriction[]
329 public static function setBlockId( $blockId, array $restrictions ) {
330 $blockRestrictions = [];
332 foreach ( $restrictions as $restriction ) {
333 if ( !$restriction instanceof Restriction
) {
337 // Clone the restriction so any references to the current restriction are
338 // not suddenly changed to a different blockId.
339 $restriction = clone $restriction;
340 $restriction->setBlockId( $blockId );
342 $blockRestrictions[] = $restriction;
345 return $blockRestrictions;
349 * Get the restrictions that should be removed, which are existing
350 * restrictions that are not in the new list of restrictions.
352 * @param Restriction[] $existing
353 * @param Restriction[] $new
356 private static function restrictionsToRemove( array $existing, array $new ) {
357 return array_filter( $existing, function ( $e ) use ( $new ) {
358 foreach ( $new as $restriction ) {
359 if ( !$restriction instanceof Restriction
) {
363 if ( $restriction->equals( $e ) ) {
373 * Converts an array of restrictions to an associative array of restrictions
374 * where the keys are the block ids.
376 * @param Restriction[] $restrictions
379 private static function restrictionsByBlockId( array $restrictions ) {
380 $blockRestrictions = [];
382 foreach ( $restrictions as $restriction ) {
383 // Ensure that all of the items in the array are restrictions.
384 if ( !$restriction instanceof Restriction
) {
388 if ( !isset( $blockRestrictions[$restriction->getBlockId()] ) ) {
389 $blockRestrictions[$restriction->getBlockId()] = [];
392 $blockRestrictions[$restriction->getBlockId()][] = $restriction;
395 return $blockRestrictions;
399 * Convert an Result Wrapper to an array of restrictions.
401 * @param IResultWrapper $result
402 * @return Restriction[]
404 private static function resultToRestrictions( IResultWrapper
$result ) {
406 foreach ( $result as $row ) {
407 $restriction = self
::rowToRestriction( $row );
409 if ( !$restriction ) {
413 $restrictions[] = $restriction;
416 return $restrictions;
420 * Convert a result row from the database into a restriction object.
422 * @param \stdClass $row
423 * @return Restriction|null
425 private static function rowToRestriction( \stdClass
$row ) {
426 if ( array_key_exists( (int)$row->ir_type
, self
::$types ) ) {
427 $class = self
::$types[ (int)$row->ir_type
];
428 return call_user_func( [ $class, 'newFromRow' ], $row );